<?php

/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Sitepagereview
 * @copyright  Copyright 2010-2011 BigStep Technologies Pvt. Ltd.
 * @license    http://www.socialengineaddons.com/license/
 * @version    $Id: IndexController.php 2011-05-05 9:40:21Z SocialEngineAddOns $
 * @author     SocialEngineAddOns
 */
class Sitepagereview_IndexController extends Core_Controller_Action_Standard {
	
	public function init() {
		if (0 !== ($page_id = ( int ) $this->_getParam ( 'page_id' )) && null !== ($sitepage = Engine_Api::_ ()->getItem ( 'sitepage_page', $page_id ))) {
			Engine_Api::_ ()->core ()->setSubject ( $sitepage );
		}
	}
	
	//ACTION FOR POSTING A REVIEW
	public function createAction() {
		$getPackageReviewCreate = Engine_Api::_ ()->sitepage ()->getPackageAuthInfo ( 'sitepagereview' );
		if ((! $this->_helper->requireUser ()->isValid ()) || empty ( $getPackageReviewCreate ))
			return;
		
		if (! $this->_helper->requireSubject ( 'sitepage_page' )->isValid ())
			return;
		
		//GET VIEWER INFORMATION
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		$viewer_id = $viewer->getIdentity ();
		
		//GET LISITING
		$this->view->sitepage = $sitepage = Engine_Api::_ ()->core ()->getSubject ();
		$this->view->page_id = $page_id = $sitepage->getIdentity ();
		
		//START MANAGE-ADMIN CHECK
		$isManageAdmin = Engine_Api::_ ()->sitepage ()->isManageAdmin ( $sitepage, 'view' );
		if (empty ( $isManageAdmin )) {
			return $this->_forward ( 'requireauth', 'error', 'core' );
		}
		//END MANAGE-ADMIN CHECK
		

		$sitepagereview_isCraete = Zend_Registry::isRegistered ( 'sitepagereview_isCraete' ) ? Zend_Registry::get ( 'sitepagereview_isCraete' ) : null;
		if (empty ( $sitepagereview_isCraete )) {
			return $this->_forward ( 'requireauth', 'error', 'core' );
		}
		$this->view->page_title_with_link = "<b>$sitepage->title</b>";
		
		$sitepageModHostName = str_replace ( 'www.', '', strtolower ( $_SERVER ['HTTP_HOST'] ) );
		
		//SET PROS-CONS AND RECOMMAND FIELD
		$this->view->prefield_recommand = 1;
		$this->view->showProsConsField = $showProsConsField = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepagereview.proscons', 1 );
		$this->view->showRecommendField = $showRecommendField = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepagereview.recommend', 1 );
		
		//VIEWER IS AUTHORIZED OR NOT FOR POSTING A REVIEW
		$hasPosted = Engine_Api::_ ()->getDbTable ( 'reviews', 'sitepagereview' )->canPostReview ( $page_id, $viewer_id );
		
		if (! empty ( $hasPosted ) || empty ( $viewer_id )) {
			return $this->_forward ( 'requireauth', 'error', 'core' );
		}
		
		//START MANAGE ADMIN AND PAGE-OWNER CAN NOT RATE & REVIEW
		$manageadmin_id = Engine_Api::_ ()->sitepagereview ()->adminCantReview ( $page_id, $viewer_id );
		if (! empty ( $manageadmin_id )) {
			return $this->_forward ( 'requireauth', 'error', 'core' );
		}
		
		//GET NAVIGATION
		$this->view->navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'sitepage_main' );
		
		//FATCH REVIEW CATEGORIES
		$this->view->reviewCategory = Engine_Api::_ ()->getDbtable ( 'reviewcats', 'sitepagereview' )->reviewParams ( $sitepage->category_id );
		$this->view->total_reviewcats = Count ( $this->view->reviewCategory );
		
		$this->view->tab_selected_id = $this->_getParam ( 'tab' );
		
		if ($this->getRequest ()->isPost ()) {
			
			$getrecommendprofile = Engine_Api::_ ()->sitepagereview ()->getRecommendProfile ();
			$this->view->prefield_title = $_POST ['title'];
			$this->view->prefield_body = $_POST ['body'];
			if (empty ( $getrecommendprofile )) {
				return;
			}
			
			$isModType = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepagereview.set.type', 0 );
			if (empty ( $isModType )) {
				Engine_Api::_ ()->getApi ( 'settings', 'core' )->setSetting ( 'sitepagereview.view.info', convert_uuencode ( $sitepageModHostName ) );
			}
			
			if (! empty ( $showRecommendField )) {
				$this->view->prefield_recommand = $_POST ['recommend'];
			}
			
			$this->view->form_error = 0;
			if (empty ( $_POST ['review_rate_0'] )) {
				
				//SHOW PRE-FIELD THE RATINGS IF OVERALL RATING IS EMPTY
				$this->view->reviewRateData = Engine_Api::_ ()->sitepagereview ()->prefieldRatingData ( $_POST );
				
				$this->view->overallrating_required = 1;
				$this->view->form_error = 1;
			
		//return;
			}
			
			if (! empty ( $showProsConsField )) {
				$this->view->prefield_pros = $_POST ['pros'];
				$this->view->prefield_cons = $_POST ['cons'];
				
				if (empty ( $_POST ['pros'] )) {
					
					//SHOW PRE-FIELD THE RATINGS IF PROS IS EMPTY
					$this->view->reviewRateData = Engine_Api::_ ()->sitepagereview ()->prefieldRatingData ( $_POST );
					
					$this->view->pros_required = 1;
					$this->view->form_error = 1;
				
		//return;
				}
				
				if (empty ( $_POST ['cons'] )) {
					
					//SHOW PRE-FIELD THE RATINGS IF CONS IS EMPTY
					$this->view->reviewRateData = Engine_Api::_ ()->sitepagereview ()->prefieldRatingData ( $_POST );
					
					$this->view->cons_required = 1;
					$this->view->form_error = 1;
				
		//return;
				}
			}
			
			if (empty ( $_POST ['title'] )) {
				
				//SHOW PRE-FIELD THE RATINGS IF TITLE IS EMPTY
				$this->view->reviewRateData = Engine_Api::_ ()->sitepagereview ()->prefieldRatingData ( $_POST );
				
				$this->view->title_required = 1;
				$this->view->form_error = 1;
			
		//return;
			}
			
			if (empty ( $_POST ['body'] )) {
				
				//SHOW PRE-FIELD THE RATINGS IF BODY IS EMPTY
				$this->view->reviewRateData = Engine_Api::_ ()->sitepagereview ()->prefieldRatingData ( $_POST );
				
				$this->view->body_required = 1;
				$this->view->form_error = 1;
			
		//return;
			}
			
			if ($this->view->form_error == 1) {
				return;
			}
			
			$db = Engine_Db_Table::getDefaultAdapter ();
			$db->beginTransaction ();
			
			try {
				
				$values = $_POST;
				$values ['owner_id'] = $viewer_id;
				$values ['page_id'] = $sitepage->getIdentity ();
				
				// Create topic
				$reviewTable = Engine_Api::_ ()->getDbtable ( 'reviews', 'sitepagereview' );
				$review = $reviewTable->createRow ();
				$review->setFromArray ( $values );
				$review->view_count = 1;
				$review->save ();
				
				//INCREASE REVIEW COUNT IN PAGE TABLE
				$sitepage->review_count ++;
				$sitepage->save ();
				
				//DO ENTRY IN REVIEW RATING TABLE
				foreach ( $_POST as $key => $ratingdata ) {
					$string_exist = strstr ( $key, 'review_rate_' );
					if ($string_exist) {
						$reviewcat_id = explode ( 'review_rate_', $key );
						$reviewRatingTable = Engine_Api::_ ()->getDbtable ( 'ratings', 'sitepagereview' );
						$reviewRating = $reviewRatingTable->createRow ();
						$reviewRating->review_id = $review->review_id;
						$reviewRating->category_id = $sitepage->category_id;
						$reviewRating->page_id = $review->page_id;
						$reviewRating->reviewcat_id = $reviewcat_id [1];
						$reviewRating->rating = $ratingdata;
						$reviewRating->save ();
					}
				}
				
				//UPDATE RATING IN PAGE TABLE
				Engine_Api::_ ()->getDbtable ( 'ratings', 'sitepagereview' )->pageRatingUpdate ( $review->page_id );
				
				$activityApi = Engine_Api::_ ()->getDbtable ( 'actions', 'activity' );
				$subject = Engine_Api::_ ()->core ()->getSubject ();
				$subjectOwner = $subject->getOwner ( 'user' );
				
				//ACTIVITY FEED
				$action = $activityApi->addActivity ( $viewer, $subject, 'sitepagereview_new' );
				
				if ($action != null) {
					$activityApi->attachActivity ( $action, $review );
				}
				
				//SENDING ACTIVITY FEED TO FACEBOOK.
				$enable_Facebooksefeed = $enable_fboldversion = Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'facebooksefeed' );
				
				if (! empty ( $enable_Facebooksefeed )) {
					
					$review_array = array ();
					$review_array ['type'] = 'sitepagereview_new';
					$review_array ['object'] = $review;
					
					Engine_Api::_ ()->facebooksefeed ()->sendFacebookFeed ( $review_array );
				}
				
				//COMMENT PRIVACY
				$auth = Engine_Api::_ ()->authorization ()->context;
				$roles = array ('owner', 'owner_member', 'owner_member_member', 'owner_network', 'registered', 'everyone' );
				$commentMax = array_search ( "everyone", $roles );
				foreach ( $roles as $i => $role ) {
					$auth->setAllowed ( $review, $role, 'comment', ($i <= $commentMax) );
				}
				
				$db->commit ();
			} catch ( Exception $e ) {
				$db->rollBack ();
				throw $e;
			}
			
			//GO TO SITEPAGE PROFILE PAGE WITH SITEPAGE REVIEW SELECTED TAB
			return $this->_helper->redirector->gotoUrl ( $review->getHref (), array ('prependBase' => false ) );
		}
	}
	
	//ACTION FOR VIEW THE REVIEW
	public function viewAction() {
		
		//GET LOGGED IN USER INFORMATION
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		$this->view->viewer_id = $viewer_id = $viewer->getIdentity ();
		if (! empty ( $viewer_id )) {
			$this->view->level_id = $viewer->level_id;
		} else {
			$this->view->level_id = 0;
		}
		
		//GET REVIEW MODEL
		$this->view->sitepagereview = $sitepagereview = Engine_Api::_ ()->getItem ( 'sitepagereview_review', $this->_getParam ( 'review_id' ) );
		if (empty ( $sitepagereview )) {
			return $this->_forward ( 'notfound', 'error', 'core' );
		}
		
		$this->view->page_id = $page_id = $sitepagereview->page_id;
		
		$this->view->sitepage = $sitepage = Engine_Api::_ ()->getItem ( 'sitepage_page', $page_id );
		
		$this->view->sitepage_slug = $sitepage->getSlug ();
		$this->view->tab_selected_id = $this->_getParam ( 'tab' );
		
		//START MANAGE-ADMIN CHECK
		$isManageAdmin = Engine_Api::_ ()->sitepage ()->isManageAdmin ( $sitepage, 'view' );
		if (empty ( $isManageAdmin )) {
			return $this->_forward ( 'requireauth', 'error', 'core' );
		}
		
		$isManageAdmin = Engine_Api::_ ()->sitepage ()->isManageAdmin ( $sitepage, 'comment' );
		if (empty ( $isManageAdmin )) {
			$this->view->can_comment = 0;
		} else {
			$this->view->can_comment = 1;
		}
		//END MANAGE-ADMIN CHECK
		

		//GET OWNER INFORMATION
		$this->view->owner = $owner = $sitepagereview->getOwner ();
		
		//INCREMENT IN NUMBER OF VIEWS
		if (! $owner->isSelf ( $viewer )) {
			$sitepagereview->view_count ++;
			$sitepagereview->save ();
		}
		
		// Start: "Suggest to Friends" link work.
		$page_flag = 0;
		$viewer_id = Engine_Api::_ ()->user ()->getViewer ()->getIdentity ();
		$is_suggestion_enabled = Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'suggestion' );
		$is_moduleEnabled = Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepage' );
		$isSupport = Engine_Api::_ ()->getApi ( 'suggestion', 'sitepage' )->isSupport ();
		if (! empty ( $is_suggestion_enabled )) {
			// Here we are delete this review suggestion if viewer have.
			if (! empty ( $is_moduleEnabled )) {
				Engine_Api::_ ()->getApi ( 'suggestion', 'sitepage' )->deleteSuggestion ( $viewer_id, 'page_review', $this->_getParam ( 'review_id' ), 'page_review_suggestion' );
			}
			
			$SuggVersion = Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->getModule ( 'suggestion' )->version;
			$versionStatus = strcasecmp ( $SuggVersion, '4.1.7p1' );
			if ($versionStatus >= 0) {
				$modContentObj = Engine_Api::_ ()->suggestion ()->getSuggestedFriend ( 'sitepagereview', $this->_getParam ( 'review_id' ), 1 );
				if (! empty ( $modContentObj )) {
					$contentCreatePopup = @COUNT ( $modContentObj );
				}
			}
			
			if (Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepage.package.enable', 1 )) {
				if ($sitepage->expiration_date <= date ( "Y-m-d H:i:s" )) {
					$page_flag = 1;
				}
			}
			if (! empty ( $contentCreatePopup ) && ! empty ( $isSupport ) && empty ( $sitepage->closed ) && ! empty ( $sitepage->approved ) && empty ( $sitepage->declined ) && ! empty ( $sitepage->draft ) && empty ( $page_flag ) && ! empty ( $viewer_id ) && ! empty ( $is_suggestion_enabled )) {
				$this->view->reviewSuggLink = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepage.review.sugg.link', 0 );
			}
		
		// End: "Suggest to Friends" link work.
		}
	}
	
	//ACTION FOR EDIT A REVIEW
	public function editAction() {
		if (! $this->_helper->requireUser ()->isValid ())
			return;
		
		if (! $this->_helper->requireSubject ( 'sitepage_page' )->isValid ())
			return;
		
		//GET REVIEW AND PAGE INFO
		$this->view->sitepage = $sitepage = Engine_Api::_ ()->core ()->getSubject ();
		$this->view->page_id = $page_id = $sitepage->getIdentity ();
		$review_id = $this->_getParam ( 'review_id' );
		$this->view->tab_selected_id = $this->_getParam ( 'tab' );
		$review = Engine_Api::_ ()->getItem ( 'sitepagereview_review', $review_id );
		$this->view->prefield_title = $review->title;
		$this->view->prefield_body = $review->body;
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		$viewer_id = $viewer->getIdentity ();
		
		//ONLY REVIEW OWNER CAN EDIT THE REVEIW
		if ($review->owner_id != $viewer_id) {
			return $this->_forward ( 'requireauth', 'error', 'core' );
		}
		
		$this->view->page_title_with_link = "<b>$sitepage->title</b>";
		
		//SHOW PROS AND CONS OR NOT
		$this->view->showProsConsField = $showProsConsField = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepagereview.proscons', 1 );
		$this->view->showRecommendField = $showRecommendField = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepagereview.recommend', 1 );
		if (! empty ( $showProsConsField )) {
			$this->view->prefield_pros = $review->pros;
			$this->view->prefield_cons = $review->cons;
		}
		if (! empty ( $showRecommendField )) {
			$this->view->prefield_recommand = $review->recommend;
		}
		
		//GET NAVIGATION
		$this->view->navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'sitepage_main' );
		
		//GET REIVEW PARAMETERS
		$this->view->reviewCategory = Engine_Api::_ ()->getDbtable ( 'reviewcats', 'sitepagereview' )->reviewParams ( $sitepage->category_id );
		$this->view->total_reviewcats = Count ( $this->view->reviewCategory );
		
		//GET THE REVIEW RATING DATAS
		$this->view->reviewRateData = Engine_Api::_ ()->getDbtable ( 'ratings', 'sitepagereview' )->ratingsData ( $review_id );
		
		//PROCESS FORM
		if ($this->getRequest ()->isPost ()) {
			
			$this->view->prefield_title = $_POST ['title'];
			$this->view->prefield_body = $_POST ['body'];
			
			if (! empty ( $showRecommendField )) {
				$this->view->prefield_recommand = $_POST ['recommend'];
			}
			
			if (empty ( $_POST ['review_rate_0'] )) {
				
				//SHOW PRE-FIELD THE RATINGS IF OVERALL RATING IS EMPTY
				$this->view->reviewRateData = Engine_Api::_ ()->sitepagereview ()->prefieldRatingData ( $_POST );
				
				$this->view->overallrating_required = 1;
				$this->view->form_error = 1;
			
		//return;
			}
			
			if (! empty ( $showProsConsField )) {
				$this->view->prefield_pros = $_POST ['pros'];
				$this->view->prefield_cons = $_POST ['cons'];
				
				if (empty ( $_POST ['pros'] )) {
					
					//SHOW PRE-FIELD THE RATINGS IF PROS IS EMPTY
					$this->view->reviewRateData = Engine_Api::_ ()->sitepagereview ()->prefieldRatingData ( $_POST );
					
					$this->view->pros_required = 1;
					$this->view->form_error = 1;
				
		//return;
				}
				
				if (empty ( $_POST ['cons'] )) {
					
					//SHOW PRE-FIELD THE RATINGS IF CONS IS EMPTY
					$this->view->reviewRateData = Engine_Api::_ ()->sitepagereview ()->prefieldRatingData ( $_POST );
					
					$this->view->cons_required = 1;
					$this->view->form_error = 1;
				
		//return;
				}
			}
			
			if (empty ( $_POST ['title'] )) {
				
				//SHOW PRE-FIELD THE RATINGS IF TITLE IS EMPTY
				$this->view->reviewRateData = Engine_Api::_ ()->sitepagereview ()->prefieldRatingData ( $_POST );
				
				$this->view->title_required = 1;
				$this->view->form_error = 1;
			
		//return;
			}
			
			if (empty ( $_POST ['body'] )) {
				
				//SHOW PRE-FIELD THE RATINGS IF BODY IS EMPTY
				$this->view->reviewRateData = Engine_Api::_ ()->sitepagereview ()->prefieldRatingData ( $_POST );
				
				$this->view->body_required = 1;
				$this->view->form_error = 1;
			
		//return;
			}
			
			if ($this->view->form_error == 1) {
				return;
			}
			Engine_Api::_ ()->sitepagereview ()->setReviewPackages ();
			$db = Engine_Db_Table::getDefaultAdapter ();
			$db->beginTransaction ();
			try {
				$values = $_POST;
				
				$values ['owner_id'] = $viewer_id;
				$values ['page_id'] = $sitepage->page_id;
				$values ['modified_date'] = date ( 'Y-m-d H:i:s' );
				
				$review->setFromArray ( $values );
				$review->save ();
				$db->commit ();
				
				$reviewRatingTable = Engine_Api::_ ()->getDbtable ( 'ratings', 'sitepagereview' );
				$reviewRatingTable->delete ( array ('review_id = ?' => $review->review_id ) );
				
				//DO ENTRY IN REVIEW RATING TABLE
				foreach ( $_POST as $key => $ratingdata ) {
					$string_exist = strstr ( $key, 'review_rate_' );
					if ($string_exist) {
						$reviewcat_id = explode ( 'review_rate_', $key );
						$reviewRatingTable = Engine_Api::_ ()->getDbtable ( 'ratings', 'sitepagereview' );
						$reviewRating = $reviewRatingTable->createRow ();
						$reviewRating->review_id = $review->review_id;
						$reviewRating->category_id = $sitepage->category_id;
						$reviewRating->page_id = $review->page_id;
						$reviewRating->reviewcat_id = $reviewcat_id [1];
						$reviewRating->rating = $ratingdata;
						$reviewRating->save ();
					}
				}
				
				//UPDATE RATING IN PAGE TABLE
				Engine_Api::_ ()->getDbtable ( 'ratings', 'sitepagereview' )->pageRatingUpdate ( $review->page_id );
			
			} catch ( Exception $e ) {
				$db->rollBack ();
				throw $e;
			}
			
			//GO TO SITEPAGE PROFILE PAGE WITH SITEPAGE REVIEW SELECTED TAB
			return $this->_helper->redirector->gotoUrl ( $review->getHref (), array ('prependBase' => false ) );
		}
	}
	
	//ACTION FOR DELETING A REVIEW
	public function deleteAction() {
		if (! $this->_helper->requireUser ()->isValid ())
			return;
		if (! $this->_helper->requireSubject ( 'sitepage_page' )->isValid ())
			return;
		
		//GET VIEWER INFORMATION
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		$viewer_id = $viewer->getIdentity ();
		$review_id = $this->_getParam ( 'review_id' );
		
		//GET REVIEW
		$review = Engine_Api::_ ()->getItem ( 'sitepagereview_review', $review_id );
		if ($review->owner_id != $viewer_id && $viewer->level_id != 1) {
			return $this->_forward ( 'requireauth', 'error', 'core' );
		}
		
		//GET NAVIGATION
		$this->view->navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'sitepage_main' );
		
		$this->view->sitepage = $sitepage = Engine_Api::_ ()->core ()->getSubject ();
		$this->view->page_id = $sitepage->page_id;
		$this->view->tab_selected_id = $tab_selected_id = $this->_getParam ( 'tab' );
		
		if ($this->getRequest ()->isPost ()) {
			
			$db = Engine_Db_Table::getDefaultAdapter ();
			$db->beginTransaction ();
			try {
				
				Engine_Api::_ ()->sitepagereview ()->deleteContent ( $review_id );
				
				$db->commit ();
			
			} catch ( Exception $e ) {
				$db->rollBack ();
				throw $e;
			}
			
			//GO TO SITEPAGE PROFILE PAGE WITH SITEPAGE REVIEW SELECTED TAB
			return $this->_helper->redirector->gotoRoute ( array ('page_url' => Engine_Api::_ ()->sitepage ()->getPageUrl ( $sitepage->getIdentity () ), 'tab' => $tab_selected_id ), 'sitepage_entry_view', true );
		} else {
			$this->renderScript ( 'index/delete.tpl' );
		}
	}

}
?>